import { Component, OnInit } from '@angular/core'
import { Router,ActivatedRoute,ActivatedRouteSnapshot } from '@angular/router'

import { PageTitle } from "./page-title"
import { Stage } from './stage'
import { StageService } from './stage.service'
import { User } from './user'
import { UserService } from './user.service'
import { Project } from './project'
import { ProjectService } from "./project.service"
import { Task } from './task'
import { TaskService } from "./task.service"

@Component({
  selector: 'task-edit',
  templateUrl: "./task-edit.component.html",
  styles: [ "" ]
})
export class TaskEditComponent implements OnInit {
  id: string
  mine: User
  project_id: string
  project: Project
  stages: Array<Stage> = [ ]
  users: Array<User>
  task: Task = new Task()
  disabled = true
  submitting = false

  constructor(private route: ActivatedRoute,
              private stageService: StageService,
              private userService: UserService,
              private taskService: TaskService,
              private projectService: ProjectService,
              private pageTitle: PageTitle,
              private router: Router) { }

  ngOnInit(): void {
    this.pageTitle.title = "任务详情"

    this.route.params.subscribe(params => {
      this.project_id = params['project_id']
      this.pageTitle.back = "/projects/" + this.project_id
      this.setStageList()
      this.id = params['task_id']
      if (this.id == "new") {
        this.pageTitle.title = "创建新任务"
        this.task = new Task()
        this.task.project_id = this.project_id
        this.projectService.get(this.project_id).then(project => {
          this.project = project
          this.task.project = project
          this.setUser()
        })
      } else
        this.taskService.get(this.id).then(task => {
          this.project_id = task.project_id
          this.pageTitle.back = "/projects/" + this.project_id
          this.project = task.project
          this.task = task
          this.setStageList()
          this.setUser()
        })
    })

    this.userService.getList().then(users => {
      this.users = users
      if (this.id == "new" && users[0])
        this.task.receiver_id = users[0].id
    })
  }

  setUser() {
    this.userService.getUserMine().then(user => {
      this.mine = user
      if (this.project.leader_id == user.id ||
          this.task.receiver_id == user.id || user.manager)
        this.disabled = false
    })
  }

  onStageChange(): void {
    this.stages.map(stage => {
      if (stage.id == this.task.stage_id)
        this.task.stage = stage
    })
  }

  onNameSelected(evn): void {
    this.task.name = evn.value
  }

  setStageList(): void {
    if (!this.project_id) return ;
    this.stageService.getList(this.project_id).then(stages => {
      this.stages = stages
      if (this.id == "new") this.task.stage_id = stages[0].id
      this.onStageChange()
    })
  }

  changeDesc(event): void {
    this.task.desc = event.target.value
  }

  submit(): void {
    this.submitting = true
    this.taskService.update(this.task).then(res => {
      this.submitting = false
      if (res.error) {
        if (res.error == "TOKEN-ERROR")
          return alert("无法处理，需要登录")
        return alert("出现错误，" + (res.msg || res.error))
      }
      this.router.navigate([ "projects", this.project_id ])
    })
  }
}
